package com.rankeiot.core.data;

import com.rankeiot.core.util.StringUtil;


import java.util.HashMap;
import java.util.Map;

/**
 * @author desire
 */
public class KvMap<K,V> extends HashMap<K,V> implements ValueGetter<V> {
    public static KvMap by(Object key, Object value) {
        return new KvMap().set(key, value);
    }
    public KvMap set(K key, V value) {
        super.put(key, value);
        return this;
    }
    public KvMap setIfNotBlank(K key, V value) {
        if (StringUtil.isNotEmpty((String)value)) {
            this.set(key, value);
        }

        return this;
    }
    public KvMap setIfNotNull(K key, V value) {
        if (value != null) {
            this.set(key, value);
        }

        return this;
    }
    public KvMap set(Map map) {
        super.putAll(map);
        return this;
    }

    public KvMap set(KvMap kv) {
        super.putAll(kv);
        return this;
    }

    public KvMap delete(Object key) {
        super.remove(key);
        return this;
    }

    /**
     * 获取本身的值，等同 get
     * @param key
     * @return
     * @param <T>
     * @see #get(Object)
     */
    public <T> T getRel(Object key) {
        return (T)this.get(key);
    }
    @Override
    public boolean equals(Object kv) {
        return kv instanceof KvMap && super.equals(kv);
    }
}
